package cn.com.tom.servlet;

import cn.com.tom.bean.Website;
import cn.com.tom.service.WebsiteService;
import cn.com.tom.service.impl.WebsiteServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/website/*")
public class WebsiteServlet extends BaseServlet {

    private WebsiteService servie = new WebsiteServiceImpl();

    public void admin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Website website = null;
        List<Website> websites = servie.list();
        if (websites != null && websites.size() > 0) {
            website = websites.get(0);
        }
        request.setAttribute("website", website);
        forward(request, response, "admin/website.jsp");
    }

    public void save(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Website website = new Website();
        BeanUtils.populate(website, request.getParameterMap());
        if (servie.save(website)) {
            redirect(request, response, "/website/admin");
        } else {
            forwardError(request, response, "保存失败");
        }
    }
}
